﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.AllowingAccessControl;
using MS.InformationManagement.BLL.AllowingAccessControl;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Web;
using System.Collections.Generic;
using MS.InformationManagement.Support.Constants;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Support;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.Text;
using System.Collections;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class AllowingAccessControl : BaseControl, IAllowingAccessControlView
    {
        #region fields

        private AllowingAccessControlPresenter _presenter;
        private static Dictionary<string, string> _ribbonActions;
        string _hideLeftColumn = "0";

        #endregion

        #region Properties

        public string HideLeftColumn { get { return _hideLeftColumn; } set { _hideLeftColumn = value; } }
        public bool IsDialog { get { return !string.IsNullOrEmpty(Page.Request.QueryString["IsDlg"]) && Page.Request.QueryString["IsDlg"] == "1" ? true : false; } }
        public bool Result { get; set; }
        public bool IsInformed { get { return !string.IsNullOrEmpty(Page.Request.QueryString["IsInformed"]) && Page.Request.QueryString["IsInformed"] == "1" ? true : false; } }
        public string CurrentUrl 
        {
            get
            {
                string url = Page.Request.ServerVariables["URL"].ToString();
                Uri uri = HttpContext.Current.Request.Url;
                return Path.GetFileName(uri.LocalPath).ToUpper();
            }
        }
        public List<Roles> AllowedRibbonActions { get; set; }
        public static Dictionary<string, string> RibbonActions
        {
            get 
            {
                if (_ribbonActions == null)
                {
                    _ribbonActions = new Dictionary<string,string>();
                    _ribbonActions.Add(GlobalConstants.ROLE_INFORM, GlobalConstants.ROLE_ID_INFORM);
                    _ribbonActions.Add(GlobalConstants.ROLE_DOCUMENT_RECEPTION, GlobalConstants.ROLE_ID_RECEPTION);
                    _ribbonActions.Add(GlobalConstants.ROLE_RADICADO_ENTRADA, GlobalConstants.ROLE_ID_RADICADO_ENTRADA);
                    _ribbonActions.Add(GlobalConstants.ROLE_RADICADO_INTERNO, GlobalConstants.ROLE_ID_RADICADO_INTERNO);
                    _ribbonActions.Add(GlobalConstants.ROLE_RADICADO_SALIDA, GlobalConstants.ROLE_ID_RADICADO_SALIDA);
                    _ribbonActions.Add(GlobalConstants.ROLE_REASIGNAR, GlobalConstants.ROLE_ID_REASIGNAR);
                    _ribbonActions.Add(GlobalConstants.ROLE_SCHEDULER, GlobalConstants.ROLE_ID_SCHEDULER);
                    _ribbonActions.Add(GlobalConstants.ROLE_UPLOAD_FILE, GlobalConstants.ROLE_ID_UPLOAD_FILE);
                    _ribbonActions.Add(GlobalConstants.ROLE_VB, GlobalConstants.ROLE_ID_VB);
                    _ribbonActions.Add(GlobalConstants.ROLE_SOLICITAR_ANULACION, GlobalConstants.ROLE_ID_SOLICITAR_ANULACION);
                    _ribbonActions.Add(GlobalConstants.ROLE_GESTION_EXPEDIENTE, GlobalConstants.ROLE_ID_GESTION_EXPEDIENTE);
                    _ribbonActions.Add(GlobalConstants.ROLE_SCHEDULE_DOCS, GlobalConstants.ROLE_ID_SCHEDULEDOCUMENT);
                    _ribbonActions.Add(GlobalConstants.ROLE_ADMINISTRATION, GlobalConstants.ROLE_ID_ADMINISTRATION);
                    _ribbonActions.Add(GlobalConstants.ROLE_SOLICITUD_PRESTAMOS, GlobalConstants.ROLE_ID_SOLICITUD_PRESTAMO);
                    _ribbonActions.Add(GlobalConstants.ROLE_INCLUIR_EXPEDIENTE, GlobalConstants.ROLE_ID_INCLUIR_EXPEDIENTE);
                    _ribbonActions.Add(GlobalConstants.ROLE_LISTADO_RADICADOS_ANULADOS, GlobalConstants.ROLE_ID_LISTADO_RADICADOS_ANULADOS);
                    _ribbonActions.Add(GlobalConstants.ROLE_SCANNING, GlobalConstants.ROLE_ID_SCANNING);
                    _ribbonActions.Add(GlobalConstants.ROLE_IMPRESION, GlobalConstants.ROLE_ID_IMPRIMIR_DOCUMENTO);
                    _ribbonActions.Add(GlobalConstants.ROLE_ENVIAR_DOCUMENTOS, GlobalConstants.ROLE_ID_ENVIAR_DOCUMENTOS);
                    _ribbonActions.Add(GlobalConstants.ROLE_DEVOLUCIONES, GlobalConstants.ROLE_ID_DEVOLUCIONES);
                    _ribbonActions.Add(GlobalConstants.ROLE_DISTRIBUCION_PENDIENTE, GlobalConstants.ROLE_ID_DISTRIBUCION);
                    _ribbonActions.Add(GlobalConstants.ROLE_MIS_DEVOLUCIONES, GlobalConstants.ROLE_ID_MIS_DEVOLUCIONES);
                    _ribbonActions.Add(GlobalConstants.ROLE_CONTACTS, GlobalConstants.ROLE_ID_CONTACTS);

                }

                return _ribbonActions;
            }
        }

        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new AllowingAccessControlPresenter(this);
            _presenter.HandleLoad();
        }
   
        #endregion

        #region Implemented Methods

        public void Show()
        {
            if (AllowedRibbonActions == null)
                return;
            Dictionary<string, string> acciones = new Dictionary<string, string>();
            SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
            string actionTab = string.Empty;
            if (ribbon != null)
            {
                foreach (var actions in RibbonActions)
                {
                    var allowedaction = (from act in AllowedRibbonActions
                                         where act.Role.ToUpper() == actions.Key.ToUpper()
                                         select act).FirstOrDefault();
                    if (allowedaction != null)
                    {
                        actionTab = allowedaction.AccionTab == null ? string.Empty : allowedaction.AccionTab.Value.ToString();
                        acciones.Add(actions.Key.ToUpper(), actionTab);
                    }
                }
            }


            WindowExtensionHelper.AddRibbonActionPage(this.Page,acciones);
             if (SPContext.Current.List != null && !SPContext.Current.List.BaseTemplate.ToString().Equals(GlobalConstants.GLOBAL_USER_LIBRARY_TEMPLATE))
                 _hideLeftColumn = "1";
             
            if (Result != true)
                SPUtility.TransferToErrorPage("Acceso denegado (" + SPContext.Current.Web.CurrentUser + ")" + CurrentUrl);


            if (AllowedRibbonActions == null)
                return;
           
            if (ribbon != null)
            {
                foreach (var actions in RibbonActions)
                {
                    var allowedaction = (from act in AllowedRibbonActions
                                         where act.Role.ToUpper() == actions.Key.ToUpper()
                                         select act).FirstOrDefault();
                    if (allowedaction == null)
                        ribbon.TrimById(RibbonActions[actions.Key]);
                }
            }


            //if (AllowedRibbonActions == null) 
            //    return;

            //SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
            //if (ribbon != null)
            //{
            //    foreach (var actions in RibbonActions)
            //    {
            //        var allowedaction =(from act in AllowedRibbonActions
            //                            where act.ToUpper() == actions.Key.ToUpper()
            //                            select act).FirstOrDefault();
            //        if(allowedaction == null)
            //            ribbon.TrimById(RibbonActions[actions.Key]);
            //    }
            //}
        }


        #endregion

    }
}
